using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using LibKetoan;
using LibDuoc;

namespace Ketoan
{
    public partial class frmDmTSCD : Form
    {
        private string sql = "";
        private string s_user = "", s_mmyy = "";
        private int i_userid = -1, i_stt;
        long l_id = -1;
        LibKetoan.AccessData a = new LibKetoan.AccessData();
        LibDuoc.AccessData s = new LibDuoc.AccessData();
        DataSet ds_data1 = null, ds_data2 = null, dsngte = null;
        DataTable tbnhom = null;
        private LibList.List liststk;
        private DataTable dttk = new DataTable();
        public frmDmTSCD(LibKetoan.AccessData acc, int iduser, string mmyy)
        {
            InitializeComponent();

            a = acc; s_mmyy = mmyy; i_userid = iduser;
        }

        private void frmDmTSCD_Load(object sender, EventArgs e)
        {
            tbnhom = a.get_DMTaikhoan("211","213").Tables[0];
            nhom.DisplayMember="TTK";
			nhom.ValueMember="ID";
            nhom.DataSource = tbnhom.DefaultView;

            dttk = a.get_data("select stk,ttk,congno,tinhchat,mlns,ctmlns,loaikm,loaikm2,loaikm3,loaikm4,id from a_dmtk where sudung=1 and nhom=11 order by stk").Tables[0];
            liststk.DisplayMember = "STK";
            liststk.ValueMember = "TTK";
            liststk.DataSource = dttk;

            khoaphong.DisplayMember = "TENKP";
            khoaphong.ValueMember = "MAKP";
            khoaphong.DataSource = a.get_data("select * from " + s.user + ".btdkp order by tenkp").Tables[0];
        }

        private void tknoa_TextChanged(object sender, EventArgs e)
        {
            if (this.ActiveControl == tknoa)
            {
                Filter_stk(tknoa.Text, liststk);
                liststk.BrowseToDmtk(tknoa, ttknoa, tkcoa, tknoa.Location.X, tknoa.Location.Y + tknoa.Height, tknoa.Width + ttknoa.Width + 2, tknoa.Height, 100);
            }
        }

        private void tknoa_Validated(object sender, EventArgs e)
        {
            DataRow r = a.getrowbyid(dttk, "stk='" + tknoa.Text + "'");
            ttknoa.Text = (r != null) ? r["ttk"].ToString() : "";
        }
        private void Filter_stk(string ma, LibList.List list)
        {
            try
            {
                CurrencyManager cm = (CurrencyManager)BindingContext[list.DataSource];
                DataView dv = (DataView)cm.List;
                sql = "stk like '" + ma.Trim() + "%' or ttk like '%" + ma.Trim() + "%'";
                dv.RowFilter = sql;
            }
            catch { }
        }

        private void butMoi_Click(object sender, EventArgs e)
        {
            l_id = 0;
            try
            {
                stt.Value = decimal.Parse(a.get_data("select max(stt) from a_dmtscd").Tables[0].Rows[0][0].ToString()) + 1;
            }
            catch { stt.Value = 1; }
            ma.Text = ten.Text = "";
            tknoa.Enabled = tkcoa.Enabled = true;
            hide.Checked = false;
            ena_object(true);           
        }
        private void ena_object(bool ena)
        {
            dataGrid1.Enabled = !ena;
            ma.Enabled = ten.Enabled = ena;            
            butMoi.Enabled = !ena;
            butSua.Enabled = !ena;
            butLuu.Enabled = ena;
            butBoqua.Enabled = ena;
            butExcel.Enabled = !ena;
            butHuy.Enabled = !ena;
            butKetthuc.Enabled = !ena;
        }

        private void tknoa_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Down || e.KeyCode == Keys.Up) liststk.Focus();
            else if (e.KeyCode == Keys.Enter)
            {
                if (liststk.Visible) liststk.Focus();
                else SendKeys.Send("{Tab}");
            }
            else if (e.KeyCode == Keys.Escape) liststk.Hide();
        }

        private void tkcoa_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Down || e.KeyCode == Keys.Up) liststk.Focus();
            else if (e.KeyCode == Keys.Enter)
            {
                if (liststk.Visible) liststk.Focus();
                else SendKeys.Send("{Tab}");
            }
            else if (e.KeyCode == Keys.Escape) liststk.Hide();
        }

        private void tkcoa_TextChanged(object sender, EventArgs e)
        {
            if (this.ActiveControl == tkcoa)
            {
                Filter_stk(tkcoa.Text, liststk);
                liststk.BrowseToDmtk(tkcoa, ttkcoa, butMoi, tkcoa.Location.X, tkcoa.Location.Y + tkcoa.Height, tkcoa.Width + ttkcoa.Width + 5, tkcoa.Height, 100);
            }
        }

        private void tkcoa_Validated(object sender, EventArgs e)
        {
            DataRow r = a.getrowbyid(dttk, "stk='" + tkcoa.Text + "'");
            ttkcoa.Text = (r != null) ? r["ttk"].ToString() : "";
        }
    }
}